from flask import Flask, jsonify, request
import uuid
import json

app = Flask(__name__)

# 加载 JSON 数据库文件
def load_database(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        return json.load(f)

# 模拟存储测评任务和答案的数据库，并添加模拟数据
assessment_db = load_database('./DBFiles/assessment_db.json')
answers_db = load_database('./DBFiles/answers_db.json')
reports_db = load_database('./DBFiles/reports_db.json')

@app.route('/start_assessment', methods=['POST'])
def start_assessment():
    # 由于没有具体的实现流程，现在手动添加模拟数据
    # data = request.get_json()
    # user_info = data.get('user_info')
    # assessment_id = str(uuid.uuid4())
    # assessment_db[assessment_id] = user_info
    user_info = {
        "user_name": "小红",
        "user_age": 25,
        "user_gender": "女"
    }
    assessment_id = str(uuid.uuid4())
    assessment_db[assessment_id] = user_info
    return jsonify({"assessment_user_info": assessment_db[assessment_id]})


@app.route('/submit_answers', methods=['POST'])
def submit_answers():
    # 由于没有具体的实现流程，现在手动添加模拟数据
    # data = request.get_json()
    # assessment_id = data.get('assessment_id')
    # answers = data.get('answers')
    assessment_id = "1"
    answers = "选项A"
    if assessment_id in assessment_db:
        answers_db[assessment_id] = answers
        return jsonify({"message": "答案提交成功"})
    else:
        return jsonify({"errCode": "no_assessment", "message": "未找到对应的测评任务"}), 200


@app.route('/generate_report', methods=['POST'])
def generate_report():
    # 由于没有具体的实现流程，现在手动添加模拟数据
    # assessment_id = request.json.get('assessment_id')
    assessment_id = "1"
    if assessment_id in assessment_db and assessment_id in answers_db:
        # 模拟生成报告
        report = f"根据您的测评，情绪状态良好，压力适中，人际关系正常。"
        reports_db[assessment_id] = report
        return jsonify({"report": report})
    else:
        return jsonify({"errCode": "no_assessment", "message": "未找到对应的测评任务或答案"}), 200


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5001)